package com.xpoint.backend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xpoint.backend.common.R;
import com.xpoint.backend.entity.Orders;
import com.xpoint.backend.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

//    下单以后清除购物车的缓存
    @CacheEvict(value = {"ShoppingCart","userOrder","adminOrder"},allEntries = true)
    @RequestMapping("/submit")
//    用户下单
    public R<String> submitOrder(@RequestBody Orders order){
        return orderService.submitOrder(order);
    }


    @Cacheable("userOrder")
    @GetMapping("/userPage")
//    用户分页查询
    public R<Page> findUserPage(Long page,Long pageSize){
        return orderService.findUserPage(page,pageSize);
    }

    @GetMapping("/page")
    @Cacheable("adminOrder")
//    后台查询订单
    public R<Page> page(Long page, Long pageSize, Long number, String beginTime, String endTime){
        return orderService.findPage(page,pageSize,number,beginTime,endTime);
    }

    @CacheEvict(value = {"ShoppingCart","userOrder","adminOrder"},allEntries = true)
    @PutMapping
    public R<String> updateStatus(@RequestBody Orders orders){
        System.out.println(orders);
        return orderService.updateStatus(orders.getStatus(),orders.getId());
    }
}
